package com.itheima.web;

import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/getUserByPageServlet")
public class GetUserByPageServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        try {
            // 1.获取请求携带的参数
            int currentPage = 1;
            int pageSize = 3;
            try {
                currentPage = Integer.parseInt(request.getParameter("currentPage"));
            } catch (NumberFormatException e) {
                currentPage = 1;
            }
            try {
                pageSize = Integer.parseInt(request.getParameter("pageSize"));
            } catch (NumberFormatException e) {
                pageSize = 3;
            }
            //2.调用service处理业务
            UserService service = new UserService();
            PageBean<User> pb = service.getUserByPage(currentPage,pageSize);
            //3.封装响应对象,并将对象转成json返回给浏览器
            Result result = new Result(true, null, pb);
            BaseController.responseJson(response,result);
        } catch (Exception e) {
            e.printStackTrace();
            Result result = new Result(false, "服务器忙,请稍后重试", null);
            BaseController.responseJson(response,result);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}